import { NextResponse } from 'next/server';
import { prisma } from '@/lib/prisma';
import { getServerSession } from 'next-auth';
import { authOptions } from '@/app/api/auth/auth.config';

export async function POST(
  request: Request,
  { params }: { params: { id: string } }
) {
  try {
    const session = await getServerSession(authOptions);
    if (!session?.user?.id) {
      return NextResponse.json({ error: '请先登录' }, { status: 401 });
    }

    const sourceTrip = await prisma.trip.findUnique({
      where: { id: params.id },
      include: { activities: true }
    });

    if (!sourceTrip) {
      return NextResponse.json(
        { error: '行程不存在' },
        { status: 404 }
      );
    }

    const newTrip = await prisma.trip.create({
      data: {
        title: `${sourceTrip.title} - 副本`,
        startDate: sourceTrip.startDate,
        endDate: sourceTrip.endDate,
        userId: session.user.id,
        activities: {
          create: sourceTrip.activities.map(activity => ({
            title: activity.title,
            date: activity.date,
            description: activity.description,
            destinationId: activity.destinationId,
          }))
        }
      }
    });

    return NextResponse.json(newTrip);
  } catch (error) {
    return NextResponse.json(
      { error: '复制失败' },
      { status: 500 }
    );
  }
} 